package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.aries.ui.view.title.TitleBarView;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.AppApplication;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseFragment;
import com.chexiehui.jc.entity.ChooseCityEvent;
import com.chexiehui.jc.ui.activity.ChooseCityActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.flyco.tablayout.SlidingTabLayout;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;

public class CarGarageFragment extends BaseFragment {

    public static CarGarageFragment getInstance() {
        CarGarageFragment fragment = new CarGarageFragment();
        return fragment;
    }

    @BindView(R.id.titleBar)
    TitleBarView titleBar;
    @BindView(R.id.vp_garage_pager)
    ViewPager vp_garage_pager;
    private SlidingTabLayout stl_car_garage_tab;
    private TextView tv_location;
    private CarGarageAssistantFragment carGarageAssistantFragment;

    private String[] titles = new String[]{"新能源", "新车", "二手车"};
    private ArrayList<Fragment> forumFragmentList = new ArrayList<>();

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_car_garage;
    }

    @Override
    protected void initTitleView(Bundle savedInstanceState) {
        View centerAction = View.inflate(mActivity, R.layout.layout_title_action_car_garage, null);
        stl_car_garage_tab = centerAction.findViewById(R.id.stl_car_garage_tab);
        tv_location = centerAction.findViewById(R.id.tv_location);
        titleBar.getTextView(Gravity.START).setVisibility(View.GONE);
        titleBar.getTextView(Gravity.END).setVisibility(View.GONE);
        titleBar.addCenterAction(titleBar.new ViewAction(centerAction));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        forumFragmentList = new ArrayList<>();
        vp_garage_pager.setOffscreenPageLimit(titles.length);
        forumFragmentList.add(CarGarageEnergyFragment.getInstance());
        forumFragmentList.add(CarGarageNewFragment.getInstance());
        forumFragmentList.add(carGarageAssistantFragment = CarGarageAssistantFragment.getInstance());
        stl_car_garage_tab.setViewPager(vp_garage_pager, titles, mActivity, forumFragmentList);
        stl_car_garage_tab.setCurrentTab(1);
        vp_garage_pager.setCurrentItem(1);
        AppApplication.getInstances().getLocationService().registerListener(mListener);
        tv_location.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                startActivity(ChooseCityActivity.class, new BundleBuilder().putInt("eventTag", 33).create());
            }
        });
    }

    @Subscribe
    public void onChooseCityEvent(ChooseCityEvent event){
        if(event != null && event.getEventTag() == 33 && event.getLocationInfo() != null){
            tv_location.setText(event.getLocationInfo().getCity());
            if(carGarageAssistantFragment != null){
                carGarageAssistantFragment.changeCity(event.getLocationInfo().getCity());
            }
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                BDLocation location = (BDLocation) msg.obj;
                tv_location.setText(location.getCity());
                if(carGarageAssistantFragment != null){
                    carGarageAssistantFragment.changeCity(location.getCity());
                }
                AppApplication.getInstances().getLocationService().unregisterListener(mListener);
            }
        }
    };

    private BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (null != location && location.getLocType() != BDLocation.TypeServerError) {
                if(StringUtils.isTrimEmpty(location.getCity())){
                    return;
                }
                Message msg = new Message();
                msg.obj = location;
                msg.what = 0;
                handler.sendMessage(msg);

            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {
            super.onConnectHotSpotMessage(s, i);
        }

        @Override
        public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
            super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
        }
    };

}
